-- 支持TP8风格的路由系统

local route_cache = {}
-- 必须确保返回模块表
local _M = {}
_M.__index = _M

-- 正确构造函数
function _M.new()
    return setmetatable({
        routes = {},
        current_group = nil
    }, _M)
end



-- 路由方法映射
local HTTP_METHODS = {
    get    = "GET",
    post   = "POST",
    put    = "PUT",
    delete = "DELETE"
}

-- 注册路由（支持链式调用）
function _M:rule(path, handler, method)
    method = method:upper()
    table.insert(route_cache, {
        method = method,
        path = path,
        handler = handler
    })
    return self -- 支持链式调用
end

-- 资源路由（RESTful）
function _M:resource(name, opts)
    local base = opts.prefix or "/"..name
    self:rule(base, name.."#index", "GET")
    self:rule(base.."/create", name.."#create", "GET")
    self:rule(base, name.."#save", "POST")
    self:rule(base.."/:id", name.."#read", "GET")
    self:rule(base.."/:id/edit", name.."#edit", "GET")
    self:rule(base.."/:id", name.."#update", "PUT")
    self:rule(base.."/:id", name.."#delete", "DELETE")
end

-- 路由分组
function _M:group(opts, callback)
    local old_prefix = self.current_prefix
    self.current_prefix = (old_prefix or "") .. (opts.prefix or "")
    
    callback(self)
    
    self.current_prefix = old_prefix
end

-- 中间件支持
function _M:middleware(middlewares)
    -- 中间件实现逻辑
end
-- 必须显式返回模块表！！！
return _M